package cn.snmp.mutual.util;
import java.util.HashMap;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.data.ResObject;
/**
 * Snmp Default constructor 
 * @author dengkn
 * */
public class DefaultSnmpHelper {

	/**
	 * @description 构建SNMP操作参数(同时支持V2C和V3)
	 * @param ne 网元对象
	 * @param mibTable MIB表名
	 * @param configFilePath MIB文件路径
	 * @return ne为空，则直接返回null
	 */
	public static ObjService getSnmpParams(ResObject ne, String mibTable,
			String configFilePath) {
		if(ne == null){
			return null;
		}	
		String version = ne.getProperty("snmpVersion");
		ObjService param = new ObjService();
		// v2c parameters as follow
		param.setValue("TableName", mibTable);
		param.setValue("ConfigFile", configFilePath);
		param.setValue("NEType", ne.getProperty("NEType"));
		param.setValue("NetNodeID", ne.getProperty("NetNodeID"));
		param.setValue("ReadCommunity", ne.getProperty("READCOM"));
		param.setValue("WriteCommunity", ne.getProperty("WRITECOM"));
		param.setValue("Timeout", ne.getProperty("TIMEOUT"));
		param.setValue("SNMPPort", ne.getProperty("PORT"));
		param.setValue("IPAddress", ne.getProperty("IPADDRESS"));
		param.setValue("Retry", ne.getProperty("RETRY"));
		param.setValue("SNMPVersion", version);

		// v3 parameters as follow
		if ("3".equalsIgnoreCase(version) || "V3".equalsIgnoreCase(version)) {
			param.setValue("ContextName", "");
			param.setValue("ValueOnly", "true");
			String v3 = ne.getProperty("V3");
			if (v3 != null) {
				// the value v3 from database: UN=shades;SL=Auth,Priv;AP=SHA;AW=11111111;PP=AES;PW=11111111
				HashMap<String, String> map = new HashMap<String, String>();
				String[] v3Params = v3.split(";");
				for (String v3Param : v3Params) {
					String[] v3Pars = v3Param.split("=");
					map.put(v3Pars[0], v3Pars[1]);
				}
				param.setValue("SnmpUserName", map.get("UN"));
				param.setValue("SecurityLevel", map.get("SL"));
				if(map.get("AP")!= null){
					param.setValue("AuthProcotol", map.get("AP"));
				}
				if(map.get("AW")!=null){
					param.setValue("AuthPassword", map.get("AW"));
				}
				if(map.get("PP")!=null){
					param.setValue("PrivProcotol", map.get("PP"));
				}
				if(map.get("PW")!=null){
					param.setValue("PrivPassword", map.get("PW"));
				}
			}
		}
		return param;
	}
			
	public static ObjService getSnmpParams(ResObject ne){
		return getSnmpParams(ne,null,null);
	}

}
